const path = require('path')

const HTMLWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
    entry: './src/index.ts', //入口文件
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js', //打包后文件名字
        environment: {
            arrowFunction: true, //webpack打包允许使用箭头函数
            const: false, //不使用const
        },
    },
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.ts$/, //指定规则生效的文件
                use: [
                    {
                        //配置babel
                        loader: 'babel-loader', //指定加载器
                        options: {
                            //设置预定义环境
                            presets: [
                                [
                                    //指定环境插件
                                    '@babel/preset-env',
                                    //配置信息
                                    {
                                        targets: {
                                            //要兼容的浏览器
                                            chrome: '88',
                                            // ie: '11',
                                        },
                                        corejs: '3', //指定corejs版本
                                        useBuiltIns: 'usage', //usage 表示按需加载
                                    },
                                ],
                            ],
                        },
                    },
                    'ts-loader',
                ],
                exclude: /node_modules/,
            },
            //处理css文件
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    //引入postcss
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: [
                                    [
                                        'postcss-preset-env',
                                        {
                                            browsers: 'last 2 versions',
                                        },
                                    ],
                                ],
                            },
                        },
                    },
                    'less-loader',
                ],
            },
        ],
    },
    plugins: [
        new HTMLWebpackPlugin({
            template: './src/index.html',
        }),
        new CleanWebpackPlugin(),
    ],
    resolve: {
        extensions: ['.ts', '.js'],
    },
}
